package com.hxh.generater.vue.simple.gen;

import com.hxh.config.Constants;
import com.hxh.entity.Project;
import com.hxh.util.GenUtil;
import com.hxh.util.DirClone;
import org.apache.commons.lang3.StringUtils;

import java.io.*;

/**
 * @ProjectName: AICode
 * @Package: com.hxh.generater
 * @Author: HXH
 * @Description:
 * @Date: 2023/11/28 15:08
 */
public class GenerationVueSystemBase {
    private static final String vueTemplatePathSuffix = "simple\\template\\";

    /**
     * 生成build目录的必要文件
     * @param project
     * @throws IOException
     */
    public static void generationBuildDir(Project project) throws IOException {
        GenUtil.newVueFile(project, "build", null);
        String sourcePath = Constants.vueTemplatePath + vueTemplatePathSuffix + "build";
        String destPath = project.getFrontendProject().getProjectDir() + project.getFrontendProject().getProjectName() + "\\build";
        DirClone dirClone = new DirClone(sourcePath, destPath);
        dirClone.Clone(sourcePath);
    }

    /**
     * 生成config目录的必要文件，配置前端项目端口
     * @param project
     * @throws IOException
     */
    public static void generationConfigDir(Project project) throws IOException {
        GenUtil.newVueFile(project, "config", null);
        String sourcePath = Constants.vueTemplatePath + vueTemplatePathSuffix + "config";
        String destPath = project.getFrontendProject().getProjectDir() + project.getFrontendProject().getProjectName() + "\\config";
        DirClone dirClone = new DirClone(sourcePath, destPath);
        dirClone.Clone(sourcePath);

        //处理端口
        File file = GenUtil.newVueFile(project, "config", "index.js");
        try {
            FileWriter fileWriter = new FileWriter(file);
            BufferedWriter writer = new BufferedWriter(fileWriter);

            File template = new File(Constants.vueTemplatePath + vueTemplatePathSuffix + "replace\\config\\index");
            BufferedReader br = new BufferedReader(new FileReader(template));
            String line;
            while ((line = br.readLine()) != null) {
                if (StringUtils.isNotBlank(line)) {
                    line = line.replaceAll("#projectPort#", project.getFrontendProject().getProjectPort());
                }
                if (StringUtils.isBlank(line)) {
                    writer.newLine();
                } else {
                    writer.write(line);
                    writer.newLine();
                }
            }
            br.close();
            writer.close();
            fileWriter.close();
            generationConfigEnv(project);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 生成config目录的必要文件，配置后台的调用地址
     * @param project
     * @throws IOException
     */
    public static void generationConfigEnv(Project project) throws IOException {
        //处理端口
        File file = GenUtil.newVueFile(project, "config", "dev.env.js");
        try {
            FileWriter fileWriter = new FileWriter(file);
            BufferedWriter writer = new BufferedWriter(fileWriter);

            File template = new File(Constants.vueTemplatePath + vueTemplatePathSuffix + "replace\\config\\dev.env");
            BufferedReader br = new BufferedReader(new FileReader(template));
            String line;
            while ((line = br.readLine()) != null) {
                if (StringUtils.isNotBlank(line)) {
                    line = line.replaceAll("#baseUrl#", project.getFrontendProject().getBaseUrl());
                }
                if (StringUtils.isBlank(line)) {
                    writer.newLine();
                } else {
                    writer.write(line);
                    writer.newLine();
                }
            }
            br.close();
            writer.close();
            fileWriter.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 生成vue项目骨架代码
     * @param project
     * @throws IOException
     */
    public static void generationSrcBaseDir(Project project) throws IOException {
        GenUtil.newVueFile(project, "static", null);

        GenUtil.newVueFile(project, "src", null);
        String sourcePath = Constants.vueTemplatePath + vueTemplatePathSuffix + "src\\assets";
        String destPath = project.getFrontendProject().getProjectDir() + project.getFrontendProject().getProjectName() + "\\src\\assets";
        DirClone dirClone = new DirClone(sourcePath, destPath);
        dirClone.Clone(sourcePath);

        sourcePath = Constants.vueTemplatePath + vueTemplatePathSuffix + "src\\api";
        destPath = project.getFrontendProject().getProjectDir() + project.getFrontendProject().getProjectName() + "\\src\\api";
        dirClone = new DirClone(sourcePath, destPath);
        dirClone.Clone(sourcePath);

        sourcePath = Constants.vueTemplatePath + vueTemplatePathSuffix + "src\\components";
        destPath = project.getFrontendProject().getProjectDir() + project.getFrontendProject().getProjectName() + "\\src\\components";
        dirClone = new DirClone(sourcePath, destPath);
        dirClone.Clone(sourcePath);

        sourcePath = Constants.vueTemplatePath + vueTemplatePathSuffix + "src\\const";
        destPath = project.getFrontendProject().getProjectDir() + project.getFrontendProject().getProjectName() + "\\src\\const";
        dirClone = new DirClone(sourcePath, destPath);
        dirClone.Clone(sourcePath);

        sourcePath = Constants.vueTemplatePath + vueTemplatePathSuffix + "src\\icons";
        destPath = project.getFrontendProject().getProjectDir() + project.getFrontendProject().getProjectName() + "\\src\\icons";
        dirClone = new DirClone(sourcePath, destPath);
        dirClone.Clone(sourcePath);

        sourcePath = Constants.vueTemplatePath + vueTemplatePathSuffix + "src\\router";
        destPath = project.getFrontendProject().getProjectDir() + project.getFrontendProject().getProjectName() + "\\src\\router";
        dirClone = new DirClone(sourcePath, destPath);
        dirClone.Clone(sourcePath);

        sourcePath = Constants.vueTemplatePath + vueTemplatePathSuffix + "src\\store";
        destPath = project.getFrontendProject().getProjectDir() + project.getFrontendProject().getProjectName() + "\\src\\store";
        dirClone = new DirClone(sourcePath, destPath);
        dirClone.Clone(sourcePath);

        sourcePath = Constants.vueTemplatePath + vueTemplatePathSuffix + "src\\styles";
        destPath = project.getFrontendProject().getProjectDir() + project.getFrontendProject().getProjectName() + "\\src\\styles";
        dirClone = new DirClone(sourcePath, destPath);
        dirClone.Clone(sourcePath);

        sourcePath = Constants.vueTemplatePath + vueTemplatePathSuffix + "src\\utils";
        destPath = project.getFrontendProject().getProjectDir() + project.getFrontendProject().getProjectName() + "\\src\\utils";
        dirClone = new DirClone(sourcePath, destPath);
        dirClone.Clone(sourcePath);

        sourcePath = Constants.vueTemplatePath + vueTemplatePathSuffix + "src\\views";
        destPath = project.getFrontendProject().getProjectDir() + project.getFrontendProject().getProjectName() + "\\src\\views";
        dirClone = new DirClone(sourcePath, destPath);
        dirClone.Clone(sourcePath);

        sourcePath = Constants.vueTemplatePath + vueTemplatePathSuffix + "src\\App.vue";
        destPath = project.getFrontendProject().getProjectDir() + project.getFrontendProject().getProjectName() + "\\src\\App.vue";
        DirClone.fileInputOutput(sourcePath, destPath);

        sourcePath = Constants.vueTemplatePath + vueTemplatePathSuffix + "src\\main.js";
        destPath = project.getFrontendProject().getProjectDir() + project.getFrontendProject().getProjectName() + "\\src\\main.js";
        DirClone.fileInputOutput(sourcePath, destPath);

        sourcePath = Constants.vueTemplatePath + vueTemplatePathSuffix + "src\\permission.js";
        destPath = project.getFrontendProject().getProjectDir() + project.getFrontendProject().getProjectName() + "\\src\\permission.js";
        DirClone.fileInputOutput(sourcePath, destPath);

    }

    /**
     * 生成index代码
     * @param project
     */
    public static void generationIndexHtml(Project project) {
        String sourcePath = Constants.vueTemplatePath + vueTemplatePathSuffix + "index.html";
        String destPath = project.getFrontendProject().getProjectDir() + project.getFrontendProject().getProjectName() + "\\index.html";
        DirClone.fileInputOutput(sourcePath, destPath);
    }

    /**
     * 生成package代码
     * @param project
     */
    public static void generationPackageJson(Project project) {
        String sourcePath = Constants.vueTemplatePath + vueTemplatePathSuffix + "package.json";
        String destPath = project.getFrontendProject().getProjectDir() + project.getFrontendProject().getProjectName() + "\\package.json";
        DirClone.fileInputOutput(sourcePath, destPath);
    }

    /**
     * 生成其他代码
     * @param project
     */
    public static void generationOtherConfig(Project project) {
        String sourcePath = Constants.vueTemplatePath + vueTemplatePathSuffix + ".babelrc";
        String destPath = project.getFrontendProject().getProjectDir() + project.getFrontendProject().getProjectName() + "\\.babelrc";
        DirClone.fileInputOutput(sourcePath, destPath);

        sourcePath = Constants.vueTemplatePath + vueTemplatePathSuffix + ".postcssrc.js";
        destPath = project.getFrontendProject().getProjectDir() + project.getFrontendProject().getProjectName() + "\\.postcssrc.js";
        DirClone.fileInputOutput(sourcePath, destPath);

        sourcePath = Constants.vueTemplatePath + vueTemplatePathSuffix + "readme.md";
        destPath = project.getFrontendProject().getProjectDir() + project.getFrontendProject().getProjectName() + "\\readme.md";
        DirClone.fileInputOutput(sourcePath, destPath);
    }

    /**
     * 初始化系统管理界面，使用内置模板
     * @param project
     */
    public static void generationInitVueSysManagerPage(Project project) {
        //user、sysUserRole、sysRoleMenu、sysRole、sysLog
        String sourcePath = Constants.vueTemplatePath + vueTemplatePathSuffix + "src\\views\\user";
        String destPath = project.getFrontendProject().getProjectDir() + project.getFrontendProject().getProjectName() + "\\src\\views\\user";
        DirClone dirClone = new DirClone(sourcePath, destPath);
        dirClone.Clone(sourcePath);

        sourcePath = Constants.vueTemplatePath + vueTemplatePathSuffix + "src\\views\\sysUserRole";
        destPath = project.getFrontendProject().getProjectDir() + project.getFrontendProject().getProjectName() + "\\src\\views\\sysUserRole";
        dirClone = new DirClone(sourcePath, destPath);
        dirClone.Clone(sourcePath);

        sourcePath = Constants.vueTemplatePath + vueTemplatePathSuffix + "src\\views\\sysRoleMenu";
        destPath = project.getFrontendProject().getProjectDir() + project.getFrontendProject().getProjectName() + "\\src\\views\\sysRoleMenu";
        dirClone = new DirClone(sourcePath, destPath);
        dirClone.Clone(sourcePath);

        sourcePath = Constants.vueTemplatePath + vueTemplatePathSuffix + "src\\views\\sysMenu";
        destPath = project.getFrontendProject().getProjectDir() + project.getFrontendProject().getProjectName() + "\\src\\views\\sysMenu";
        dirClone = new DirClone(sourcePath, destPath);
        dirClone.Clone(sourcePath);

        sourcePath = Constants.vueTemplatePath + vueTemplatePathSuffix + "src\\views\\sysRole";
        destPath = project.getFrontendProject().getProjectDir() + project.getFrontendProject().getProjectName() + "\\src\\views\\sysRole";
        dirClone = new DirClone(sourcePath, destPath);
        dirClone.Clone(sourcePath);

        sourcePath = Constants.vueTemplatePath + vueTemplatePathSuffix + "src\\views\\sysLog";
        destPath = project.getFrontendProject().getProjectDir() + project.getFrontendProject().getProjectName() + "\\src\\views\\sysLog";
        dirClone = new DirClone(sourcePath, destPath);
        dirClone.Clone(sourcePath);
        //组件
        sourcePath = Constants.vueTemplatePath + vueTemplatePathSuffix + "src\\views\\component";
        destPath = project.getFrontendProject().getProjectDir() + project.getFrontendProject().getProjectName() + "\\src\\views\\component";
        dirClone = new DirClone(sourcePath, destPath);
        dirClone.Clone(sourcePath);
    }

    /**
     * 初始化导入导出界面，使用内置模板
     * @param project
     */
    public static void generationInitVueImportExportPage(Project project) {
        //batchImportExportConfig
        String sourcePath = Constants.vueTemplatePath + vueTemplatePathSuffix + "src\\views\\batchImportExportConfig";
        String destPath = project.getFrontendProject().getProjectDir() + project.getFrontendProject().getProjectName() + "\\src\\views\\batchImportExportConfig";
        DirClone dirClone = new DirClone(sourcePath, destPath);
        dirClone.Clone(sourcePath);
    }
}
